<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="调入库间id" prop="toShid">-->
<!--        <el-input-->
<!--          v-model="queryParams.toShid"-->
<!--          placeholder="请输入调入库间id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="1 – 启用 0 – 禁用" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择1 – 启用 0 – 禁用" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:transfer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:transfer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:transfer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:transfer:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="调出组织" align="center" prop="fromOrgname" />
      <el-table-column label="调出库房" align="center" prop="fromStrmname" />
      <el-table-column label="调出库间" align="center" prop="fromShname" />
      <el-table-column label="调入组织" align="center" prop="toOrgname" />
      <el-table-column label="调入库房" align="center" prop="toStrmname" />
      <el-table-column label="调入库间" align="center" prop="toShname" />
      <el-table-column label="部门" align="center" prop="deptName" />
<!--      <el-table-column label="提交人" align="center" prop="submitter"/>-->
      <el-table-column label="分组标识" align="center" prop="directSaleGroup"/>
      <el-table-column label="顺序" align="center" prop="sortNum"/>
      <el-table-column label="状态" align="center" prop="statusname" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bill:transfer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bill:transfer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自动调拨对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="调出组织" prop="fromOrgname">
          <el-select size="small" placeholder="请选择调出组织" clearable filterable v-model="form.fromOrgguid">
            <el-option v-for="item in orgs" :key="item.guid" :label="item.name" :value="item.guid" @click.native="getStrm($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="调出库房" prop="fromStrmname">
          <el-select size="small" filterable clearable placeholder="请选择调出库房" v-model="form.fromStrmguid">
            <el-option v-for="item in strms" :key="item.guid" :label="item.name" :value="item.guid" @click.native="getShrm($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="调出库间" prop="fromShname">
          <el-select size="small" placeholder="请选择调出库间" clearable filterable v-model="form.fromShguid">
            <el-option v-for="item in shrms" :key="item.guid" :label="item.name" :value="item.guid" @click.native="fromstrhChange($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="调入组织" prop="toOrgname">
          <el-select size="small" placeholder="请选择调入组织" clearable filterable v-model="form.toOrguid">
            <el-option v-for="item in orgs" :key="item.guid" :label="item.name" :value="item.guid" @click.native="getToStrm($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="调入库房" prop="toStrmname">
          <el-select size="small" filterable clearable placeholder="请选择调入库房" v-model="form.toStrmguid">
            <el-option v-for="item in tostrms" :key="item.guid" :label="item.name" :value="item.guid" @click.native="getToShrm($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="调入库间" prop="toShname" >
          <el-select size="small" placeholder="请选择调入库间" clearable filterable v-model="form.toShguid">
            <el-option v-for="item in toshrms" :key="item.guid" :label="item.name" :value="item.guid" @click.native="tostrhChange($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="deptName">
          <el-select size="small" placeholder="请选择部门" clearable filterable v-model="form.deptId">
            <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" @click.native="deptChange($event,item)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="提交人" prop="submitter">
          <el-select size="small" placeholder="请选择提交人" clearable filterable v-model="form.submitter">
            <el-option v-for="item in users" :key="item.userId" :label="item.nickName" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="最低调拨库存" prop="minCount">
          <el-input v-model="form.minCount" placeholder="请输入最低调拨库存" style="width: auto"/>
        </el-form-item>
        <el-form-item label="分组标识" prop="directSaleGroup">
          <el-input v-model="form.directSaleGroup" placeholder="请输入分组标识" />
        </el-form-item>
        <el-form-item label="顺序" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入顺序" style="width: auto"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" >
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransfer, getTransfer, delTransfer, addTransfer, updateTransfer, exportTransfer } from "@/api/bill/autoTransfer";
import {findDepartment,findOrganizationList,findStoreroom,findStoreroomListByPermiss,findStorehouse,getDepartmentList,getUserList} from '@/api/public'

export default {
  name: "autoTransfer",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自动调拨表格数据
      transferList: [
      ],
      orgs:[],
      strms:[],
      shrms:[],
      tostrms:[],
      toshrms:[],
      deptList:[],
      users:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fromOrgid: null,
        fromOrgname: null,
        fromStrmid: null,
        fromStrmname: null,
        fromShid: null,
        fromShname: null,
        toOrgid: null,
        toOrgname: null,
        toStrmid: null,
        toStrmname: null,
        toShid: null,
        toShname: null,
        minCount: null,
        sortNum: null,
        status: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getUser();
    this.getList();
    this.getOrgs();
    this.getDept();
  },
  methods: {
    deptChange(event,item){
      this.form.deptName=item.name;
    },
    tostrhChange(event,item){
      this.form.toShname=item.name;
    },
    fromstrhChange(event,item){
      this.form.fromShname=item.name;
    },
    getUser(){
      getUserList().then(response=>{
        this.users=response.data;
      })
    },
    getDept(){
      getDepartmentList().then(response=>{
        this.deptList=response.data;
      })
    },
    getToShrm(event,item){
      this.form.toStrmname=item.name;
      findStorehouse({storeguid:item.guid}).then(response=>{
        this.toshrms=response.data;
      })
    },
    getToStrm(event,item){
      this.form.toStrmguid='';
      this.form.toOrgname=item.name;
      this.tostrms=[];
      findStoreroom({orgguid:item.guid}).then(response=>{
        this.tostrms=response.data;
      })
    },
    getShrm(event,item){
      this.form.fromStrmname=item.name;
      findStorehouse({storeguid:item.guid}).then(response=>{
        this.shrms=response.data;
      })
    },
    getStrm(event,item){
      this.form.fromStrmguid='';
      this.form.fromOrgname=item.name;
      this.shrms=[];
      findStoreroom({orgguid:item.guid}).then(response=>{
        this.strms=response.data;
      })
    },
    getOrgs(){
      findOrganizationList().then(response=>{
        this.orgs=response.data;
      })
    },
    /** 查询自动调拨列表 */
    getList() {
      this.loading = true;
      listTransfer(this.queryParams).then(response => {
        if (response.code==200){
          response.rows.map(item=>{
            item.status==1?item.statusname='启用':item.statusname='禁用';
          })
          this.transferList = response.rows;
          // console.log("users:------")
          // console.log(this.users)
          // this.transferList.submitterName=this.users.find(item=>item.userId=this.transferList.submitter).nickName;
          this.total = response.total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fromOrgid: null,
        fromOrgname: null,
        fromStrmid: null,
        fromStrmname: null,
        fromShid: null,
        fromShname: null,
        toOrgid: null,
        toOrgname: null,
        toStrmid: null,
        toStrmname: null,
        toShid: null,
        toShname: null,
        minCount: null,
        sortNum: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自动调拨";
      this.form.status=1
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTransfer(id).then(response => {
        this.form = response.data;
        findStoreroom({orgguid:this.form.fromOrgguid}).then(response=>{
          this.strms=response.data;
        })
        findStoreroom({orgguid:this.form.toOrguid}).then(response=>{
          this.tostrms=response.data;
        })
        findStorehouse({storeguid:this.form.fromStrmguid}).then(response=>{
          this.shrms=response.data;
        })
        findStorehouse({storeguid:this.form.toStrmguid}).then(response=>{
          this.toshrms=response.data;
        })
        this.open = true;
        this.title = "修改自动调拨";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // console.log(this.form)
          if (this.form.id != null) {
            updateTransfer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransfer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTransfer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有自动调拨数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTransfer(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
